<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>卖掉房东的房子去躺平模拟器</title>
</head>

<body style="">
<h1>卖掉房东的房子去躺平模拟器</h1>

请输入卖掉房东的房子所得金额<br/>
<input id="price" type="number" value="1000000">元

<br/>
<br/>
请输入银行年利率(微信零钱通为1.89%)<br/>
<input id="lilv" type="number" value="1.89">%

<br/>
<br/>
每年初取出多少钱给自己花<br/>
<input id="hua" type="number" value="60000">元

<br/><br/>
<button style="width: 200px;height: 30px" id="button_start">幻想时间</button>
<br/>
<div id="record"></div>

<br/>
<br/>
<button style="width: 200px;height: 30px;display: none" id="button_reset">重置</button>

<script>

    //展示重置按钮
    document.getElementById("button_reset").onclick = function () {
        //刷新当前网页
        window.location.reload();
    }

    //点击按钮
    document.getElementById("button_start").onclick = function () {
        //隐藏id为button_start按钮
        document.getElementById("button_start").style.display="none";
        //展示重置按钮
        document.getElementById("button_reset").style.display="inline-block";


        let initialCapital = document.getElementById("price").value; // 初始本金
        //把initialCapital转成数字类型
        initialCapital = Number(initialCapital);
        // console.log(initialCapital);
        // console.log(typeof initialCapital);
        let annualInterestRate = (document.getElementById("lilv").value / 100).toFixed(4); // 年利率
        //把initialCapital转成数字类型
        annualInterestRate = Number(annualInterestRate);
        // console.log(annualInterestRate);
        // console.log(typeof annualInterestRate);
        let annualWithdrawal = document.getElementById("hua").value; // 每年底取出金额
        //把annualWithdrawal转成数字类型
        annualWithdrawal = Number(annualWithdrawal);
        // console.log(annualWithdrawal);
        // console.log(typeof annualWithdrawal);
        // 定义存储每年剩余金额的数组
        let remainingAmounts = [];

        // 计算每年剩余金额
        for (let year = 1; year<=1000 ; year++) {
            // 如果取款大于年初总金额，则当年剩余金额为0，结束循环
            if (annualWithdrawal > initialCapital) {
                remainingAmounts.push(initialCapital - annualWithdrawal);
                break;
            } else {
                // 取款后计算当年剩余金额
                initialCapital = initialCapital - annualWithdrawal;

                // 将当年剩余金额存入数组
                remainingAmounts.push(initialCapital);

                // 如果剩余本金小于等于0，则停止计算
                if (initialCapital <= 0) {
                    break;
                }
            }

            // 计算本年利息（基于上一年末的本金）
            let interest = initialCapital * annualInterestRate;

            // 计算当年年初总金额（加上利息）
            initialCapital = initialCapital + interest;

        }

        //输出每年剩余金额
        for (let i = 0; i < remainingAmounts.length; i++) {
            if (remainingAmounts[i] > 0) {
                console.log(`第${i + 1}年初取出后，剩余金额为：${remainingAmounts[i].toFixed(2)}元`);
                document.getElementById("record").innerHTML += "<br/>" +`第${i + 1}年初取出后，剩余金额为：${remainingAmounts[i].toFixed(2)}元`;
            } else {
                console.log(`第${i + 1}年初，剩余金额存款已不够取出了`);
                document.getElementById("record").innerHTML += "<br/>" + `第${i + 1}年初，剩余金额存款已不够取出了`;
            }
            if(i==999){
                document.getElementById("record").innerHTML += "<br/>" + "大哥! 您的钱1000年都没花完，带带小弟弟~";
            }
        }
    }

</script>
</body>
</html>
